export default {
  // 将数据存储在本地缓存中指定的 name 中
  setLocal (name, data) {
    try {
      uni.setStorageSync(name, data)
    } catch (e) {
      console.log(e)
    }
  },
  // 从本地缓存中获取指定 name 对应的内容
  getLocal (name) {
    let data
    try {
      data = uni.getStorageSync(name)
    } catch (e) {
      console.log(e)
    }
    return data
  },
  // 从本地缓存中移除指定 key
  deleteLocal (name) {
    try {
      uni.removeStorageSync(name)
    } catch (e) {
      console.log(e)
    }
  },
  // 返回一个布尔值，表示 name 是否在本地缓存之中
  hasLocal (name) {
    const value = ''
    try {
      const res = uni.getStorageInfoSync()
      value = res.keys.includes(name)
    } catch (e) {
      console.log(e)
    }
    return value
  },
  // 清理本地数据缓存
  clearLocal () {
    try {
      uni.clearStorageSync()
    } catch (e) {
      console.log(e)
    }
  },
	
	valid (rules,form){ //表单验证
      var rules = rules;
      var form = form;
      for(var key in rules){
        for(var i=0;i<rules[key].length;i++){
          if(rules[key][i].required){
            if(form[key] === ""){
			  uni.showToast({
			  	icon:'none',
				title:rules[key][i].message
			  })
              return false;
            }
			if(typeof form[key] == "object" && !form[key].length){
			  uni.showToast({
			  	icon:'none',
				title:rules[key][i].message
			  })
			  return false;
			}
          }else if(rules[key][i].pattern){
            if(!rules[key][i].pattern.test(form[key])){
              uni.showToast({
              	icon:'none',
              	title:rules[key][i].message
              })
              return false;
            }
          }else if(rules[key][i].minLength){
            if(form[key].length < rules[key][i].minLength){
              uni.showToast({
              	icon:'none',
              	title:rules[key][i].message
              })
              return false;
            }
          }else if(rules[key][i].maxLength){
            if(form[key].length > rules[key][i].maxLength){
              uni.showToast({
              	icon:'none',
              	title:rules[key][i].message
              })
              return false;
            }
          }else if(rules[key][i].validator){
            if(typeof rules[key][i].validator === 'function'){
              if(!rules[key][i].validator()){
                return false;
              }
            }
          }
        }
      }
      return true;
    }
}